<#--信息视图控制层control：Action类的定义-->
<#include "hihsoft_agreement.include">
<#include "custom.include">
<#assign className = table.className>  
<#assign idColumn = table.idColumn ?lower_case>
<#assign idUpper = idColumn ?cap_first>
<#assign classNameLower = className?uncap_first>
<#assign jspfileName=className?lower_case> 
<#assign jspDir1=table.sqlName?lower_case> 
<#assign jspDir2=jspDir1?length> 
<#assign jspDir3=jspDir1?last_index_of("_")> 
<#assign jspDir=jspDir1?substring(jspDir3+1,jspDir2)> 
package ${basepackage}.${subpackage}.web.controller;
import com.hihframework.core.utils.StringHelpers;
import com.hihframework.exception.ControllerException;
import com.hihsoft.baseclass.web.controller.javahihBaseController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.util.WebUtils;

import ${basepackage}.${subpackage}.service.${className}Service;
import ${basepackage}.${subpackage}.model.*;

<#include "java_copyright.include">
@Controller
@RequestMapping("/${classNameLower}Controller.do")
public class ${className}Controller extends javahihBaseController {
	@Autowired
	private ${className}Service ${classNameLower}Service;
	
	/**
	* 访问列表
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	@RequestMapping(params="method=list")
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws ControllerException {
		ModelAndView mv = new ModelAndView("/${jspDir}/${jspfileName}list");
		String hql = "from ${className}";
		Map<String, Object> filter = WebUtils.getParametersStartingWith(
				request, "srh_");// 得到查询条件
		String orders = getParam(request, "orders");
		Set<String> set = filter.keySet();
		for (Iterator<String> it = set.iterator(); it.hasNext();) {
			String key = it.next();
			Object value = filter.get(key);
			if (StringHelpers.notNull(value)) {
				hql += " and " + key + "='" + value + "'";
			}
		}
		if (orders != null && !"".equals(orders))
			hql += " order by " + orders;
		addOrders(request, orders);
		List<${className}> list = calcPage(request, ${classNameLower}Service, hql);
		mv.addObject("list", list);
	    return mv;
	}
	
	/**
	* 進入新增頁面
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	@RequestMapping(params="method=add")
	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response) throws ControllerException {
		return new ModelAndView("/${jspDir}/${jspfileName}form");
	}
	/**
	 * 修改
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(params="method=modify")
	public ModelAndView modify(HttpServletRequest request,
			HttpServletResponse response) throws ControllerException {
		ModelAndView mv = new ModelAndView("/${jspDir}/${jspfileName}form");
		String id=request.getParameter("id");
		${className} ${classNameLower}=${classNameLower}Service.get${className}ById(id);
	    mv.addObject("${classNameLower}", ${classNameLower});
		return mv;
	}
	/**
	* 查看记录详细
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	@RequestMapping(params="method=view")
	public ModelAndView view(HttpServletRequest request,
			HttpServletResponse response) throws ControllerException {
		String id=request.getParameter("id");
		${className} ${classNameLower}=${classNameLower}Service.get${className}ById(id);
	    this.bind(request,${classNameLower});
		return new ModelAndView("/${jspDir}/${jspfileName}form");
	}
	
	/**
	* 保存
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	@RequestMapping(params="method=save")
	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response) throws ControllerException {
		${className} ${classNameLower}=new ${className}();
	    this.setValue(request,${classNameLower});
	    ${classNameLower}Service.saveOrUpdate${className}(${classNameLower});
	    return new ModelAndView(new RedirectView(request.getContextPath()+"/${classNameLower}Controller.do?method=list"));
	}
	
	/**
	* 删除
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	@RequestMapping(params="method=delete")
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws ControllerException {
		String ids = request.getParameter("ids");
		boolean flag = true;
		List<${className}> list = new ArrayList<${className}>();
		if (ids != null && !"".equals(ids)) {
			String[] arr = ids.split(",");
			for (String id : arr) {
				${className} obj = new ${className}();
				obj.set${idUpper}(id);
				list.add(obj);
			}
		}
		try {
			${classNameLower}Service.deleteBatchVO(list);
		} catch (Exception e) {
			flag = false;
			logger.error(e);
		}
		renderJson(response, toJson("success", flag));
		return null;
	}
	
}

